﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;

namespace QuanLyHopDong
{
    public partial class ChucVuListView : Form
    {
        private BindingList<ChucVu> bindingListChucVu;
        
        private ChucVu _chucVu;

        public ChucVuListView()
        {
            InitializeComponent();
        }

        private void ChucVuListView_Load(object sender, EventArgs e)
        {
            AppGridView.InitGridView(gridView1,false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gridView1);
            AppGridView.ShowField(gridView1, new string[] { "MaQuanLy", "TenChucVu" }, new string[] { "Mã Quản Lý", "Tên Chức Vụ" });

            LoadData();

        }

        private void LoadData()
        {
            List<ChucVu> list = DataAccessObject.ChucVu.GetListChucVu();
            bindingListChucVu = new BindingList<ChucVu>(list);
            
            bindingSource1.DataSource = bindingListChucVu;
            gvChucVu.Invalidate();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _chucVu = new ChucVu();
            OpenDetailForm();
        }


        private void OpenDetailForm()
        {
            ChucVuDetailView detailView = new ChucVuDetailView(_chucVu);
            detailView.SaveChanged += detailView_SaveChanged;
            detailView.DeleteChanged += new EventHandler(detailView_DeleteChanged);
            detailView.MdiParent = this.MdiParent;
            detailView.Show();
        }
        void detailView_DeleteChanged(object sender, EventArgs e)
        {
            if (_chucVu != null)
            {
                if (DataAccessObject.ChucVu.Delete(_chucVu) == 1)
                {
                    gridView1.DeleteRow(gridView1.FocusedRowHandle);
                    gvChucVu.Invalidate();
                }
            }
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            bindingListChucVu.Add(_chucVu);
            gvChucVu.Invalidate();
        }
        private void Delete()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _chucVu = gridView1.GetFocusedRow() as ChucVu;
                if (_chucVu != null && _chucVu.Oid > 0)
                {
                    if (DataAccessObject.ChucVu.Delete(_chucVu) == 1)
                    {
                        gridView1.DeleteRow(gridView1.FocusedRowHandle);
                        gvChucVu.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void gvChucVu_DoubleClick(object sender, EventArgs e)
        {
            _chucVu = gridView1.GetFocusedRow() as ChucVu;
            if (_chucVu != null)
                OpenDetailForm();
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
        }

        private void gvChucVu_Click(object sender, EventArgs e)
        {

        }
    }
}
